<!DOCTYPE html>
<html lang="zh-cn">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>主页</title>
    <link rel="stylesheet" href="/plugins/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/plugins/build/css/app.css" media="all">
</head>

<body>
<div class="container">
    <form class="layui-form" action="">
        <input type="hidden" name="classifyId" value=""/>
        <div class="layui-form-item">
            <label class="layui-form-label">分类名称</label>
            <div class="layui-input-block">
                <input type="text" name="classifyName" required  lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item custom_layer_bottom_btn">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="save">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script src="/plugins/layui/layui.js"></script>
<script src="/plugins/common.js"></script>
<script>
    //配置项：详情地址、添加地址
    var detailUrl = SERVER_URL+"/shopClassify/detail";
    var editUrl = SERVER_URL+"/shopClassify/edit";

    layui.use(['jquery', 'form'], function() {
        var $ = layui.jquery;
        var form = layui.form;

        //配置项：初始化表单数据
        function initForm(data) {
            $("input[name='classifyId']").val(data.classifyId);
            $("input[name='classifyName']").val(data.classifyName);
        }

        //判断新增or修改
        if(urlParam("id")){
            $.ajax({
                type: 'GET',
                url: detailUrl,
                data: {id: urlParam("id")},
                success: function (result) {
                    if(result.code == 20000){
                        initForm(result.data);
                    }else{
                        layer.msg(result.msg, {time: 1500});
                    }
                }
            });
        }

        //新增or修改
        form.on('submit(save)', function(data){
            $.ajax({
                type: 'POST',
                url: editUrl,
                data: data.field,
                success: function (result) {
                    if(result.code == 20000){
                        parent.reload();
                    }else{
                        layer.msg(result.msg, {time: 1500});
                    }
                }
            });
            return false;
        });
    });

    //地址栏参数
    function urlParam(name){
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if(r!=null)	return unescape(r[2]);
        return null;
    }
</script>
</body>

</html>
